package com.kwlt.smc.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;

import com.kwlt.smc.entity.SmsSendDetail;
import com.kwlt.smc.util.ConUtils;


@Component
public class RptSendDetailDaoImpl implements RptSendDetailDao{

	@PersistenceContext
	private EntityManager em;
	@Override
	public void create(String tableName) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("CREATE TABLE IF NOT EXISTS ").append(tableName);
		sql.append(" LIKE rpt_send_detail_yyyymmdd");
		try {
			em.createNativeQuery(sql.toString()).executeUpdate();
		} catch (Exception e) {
			throw e;
		}
		
	}

	@Override
	public List query(String sql) {
		List result = em.createNativeQuery(sql.toString()).getResultList();
		return result;
	}

	public void save(List<SmsSendDetail> list, String date) {
		String tableName = "rpt_send_detail_"+date;
		insert(list,tableName);
	}

	private void insert(List<SmsSendDetail> list,String tableName){
		if(list!=null && list.size()>0){
			StringBuffer sql = new StringBuffer();
			sql.append("INSERT INTO ").append(tableName).append("(");
			sql.append("PHONE_NUMBER,SEND_DATE,SEND_SUCCESS_FLAG,RPT_FLAG,RPT_VALUE,USER,RPT_DATE,USER_DATE,");
			sql.append("CHANNEL,INT_SUBMIT_DATE,SEND_RESP_CODE,INT_MSG_ID,LOCAL_MSG_ID,OPER_BAK_MSG_ID,OPERATORS) VALUES  ");
			for(int i=0;i<list.size();i++){
				SmsSendDetail po = list.get(i);
				sql.append("('"+po.getPhoneNum()).append("',");
				sql.append(po.getSendDate()==null?"null":"'"+ConUtils.formatDate(po.getSendDate(), "yyyyMMddHHmmss")+"'").append(",");
				sql.append(po.getSendSuccessFlag()==null?"null":"'"+po.getSendSuccessFlag()+"'").append(",");
				sql.append(po.getRptFlag()==null?"null":"'"+po.getRptFlag()+"'").append(",");
				sql.append(po.getRptValue()==null?"null":"'"+po.getRptValue()+"'").append(",");
				sql.append(po.getUser()==null?"null":"'"+po.getUser()+"'").append(",");
				sql.append(po.getRptDate()==null?"null":"'"+ConUtils.formatDate(po.getRptDate(), "yyyyMMddHHmmss")+"'").append(",");
				sql.append(po.getUserDate()==null?"null":"'"+po.getUserDate()+"'").append(",");
				sql.append(po.getChannel()==null?"null":"'"+po.getChannel()+"'").append(",");
				sql.append(po.getIntSubmitDate()==null?"null":"'"+ConUtils.formatDate(po.getIntSubmitDate(), "yyyyMMddHHmmss")+"'").append(",");
				sql.append(po.getSendRespCode()==null?"null":"'"+po.getSendRespCode()+"'").append(",");
				sql.append(po.getIntMsgId()==null?"null":"'"+po.getIntMsgId()+"'").append(",");
				sql.append(po.getLocalMsgId()==null?"null":"'"+po.getLocalMsgId()+"'").append(",");
				sql.append(po.getOperBakMsgId()==null?"null":"'"+po.getOperBakMsgId()+"'").append(",");
				sql.append(po.getOperators()==null?"null":"'"+po.getOperators()+"'");
				sql.append(")");
				if(list.size()-i>1){
					sql.append(",");
				}
			}
			Session session=null;
			try {
				session = (Session) em.getEntityManagerFactory().createEntityManager().getDelegate();
				Transaction tc = session.beginTransaction();
				org.hibernate.Query createQuery = session.createSQLQuery(sql.toString());
				createQuery.executeUpdate();
				tc.commit();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(session!=null){
					session.clear();
					session.close();
				}
			}
			
			
		}
	}

	@Override
	public boolean getRptSendDetailByLocalMsgId(String localMsgId,
			String phoneNumber, String dateStr) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT 1 FROM ").append("rpt_send_detail_" + dateStr);
		sql.append(" where local_msg_id='" + localMsgId + "' and phone_number='"+phoneNumber+"' ");
		SmsSendDetail sd=null;
		try {
			List list = em.createNativeQuery(sql.toString()).getResultList();
			if (list.isEmpty()) {
				return true;
			} else{
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return true;
		}
	}
	
	@Override
	public SmsSendDetail getRptByLocalMsgIdaAndPhone(String localmsgid, String phone,
			String tableName) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from ").append(tableName);
		sql.append(" a where a.LOCAL_MSG_ID = '").append(localmsgid).append("' ");
		sql.append(" and a.PHONE_NUMBER = '").append(phone).append("'");
		
		SmsSendDetail sd=null;
		try {
			List list = em.createNativeQuery(sql.toString()).getResultList();
			if (list.isEmpty()) {
				return null;
			} else{
				Object[] obj = (Object[]) list.get(0);
				sd = new SmsSendDetail();
				sd.setPhoneNum(String.valueOf(obj[1]));
				sd.setRptValue(String.valueOf(obj[5]));
				sd.setLocalMsgId(String.valueOf(obj[13]));
				return sd;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
